package com.molyfun.parents.activity.coursecenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.rest.bean.GetSubjectResponse;
import com.molyfun.parents.zhibo.ZhiboActivity;
import com.molyfun.parents.zhibo.playback.PlayBackActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TargetDetailActivity extends BaseActivity implements View.OnClickListener {

    public static final String SUBJECT_ID = "subject_id";
    public static final String PRODUCT_ID = "product_id";

    @InjectView(R.id.base_title_left)
    ImageView mTitleLeft;
    @InjectView(R.id.title)
    TextView mTitle;
    @InjectView(R.id.target_title)
    TextView mTargetTitle;
    @InjectView(R.id.target_content)
    TextView mTargetContent;
    @InjectView(R.id.target_detail_attachmentviewer)
    AttachmentViewer mAttachmentViewer;
    @InjectView(R.id.view_zhibo)
    TextView mViewZhibo;

    private String mProductId = "";
    private String mSubjectId;

    private GetSubjectResponse.SubjectDetailBean subjectDetailBean;

    public static void startActivity(Context context, String productId, String subjectId){
        Intent intent = new Intent();
        intent.setClass(context, TargetDetailActivity.class);
        intent.putExtra(PRODUCT_ID, productId);
        intent.putExtra(SUBJECT_ID, subjectId);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target_detail);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");
        mTitleLeft.setOnClickListener(this);
        if (savedInstanceState != null) {
            mProductId = savedInstanceState.getString(PRODUCT_ID);
            mSubjectId = savedInstanceState.getString(SUBJECT_ID);
        }else{
            mProductId = getIntent().getStringExtra(PRODUCT_ID);
            mSubjectId = getIntent().getStringExtra(SUBJECT_ID);
        }

        if(!TextUtils.isEmpty(mProductId)){
            getSubjectCatalogInfo(mProductId);
        }
        mViewZhibo.setOnClickListener(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(PRODUCT_ID, mProductId);
        outState.putString(SUBJECT_ID, mSubjectId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
            case R.id.view_zhibo:
                if(subjectDetailBean != null){
                    if(subjectDetailBean.getLivestatus().equals("playback")){
                        if(!TextUtils.isEmpty(subjectDetailBean.getRecordurl())){
                            PlayBackActivity.startActivity(TargetDetailActivity.this, subjectDetailBean.getId(), subjectDetailBean.getName(), subjectDetailBean.getRecordurl(), subjectDetailBean.getTarget(), mSubjectId);
                        }
                    }else{
                        ZhiboActivity.startActivity(TargetDetailActivity.this, subjectDetailBean.getId(), subjectDetailBean.getName(), mSubjectId);
                    }
                }
                break;
        }
    }

    private void getSubjectCatalogInfo(String productId) {
        showProgressDialog();
        Call<GetSubjectResponse> call = MFPApplication.getInstance().getRestClient().getSubjectCatalogInfo(productId);
        call.enqueue(new Callback<GetSubjectResponse>() {
            @Override
            public void onResponse(Call<GetSubjectResponse> call, Response<GetSubjectResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200 && response.body().getData() != null) {
                        subjectDetailBean = response.body().getData();
                        mTitle.setText(subjectDetailBean.getName());
                        mTargetTitle.setText(subjectDetailBean.getTarget());
                        mTargetContent.setText(subjectDetailBean.getProcess());
                        mAttachmentViewer.initData(subjectDetailBean.getSubjectcourselist());
                        if(TextUtils.isEmpty(subjectDetailBean.getLivestatus()) || subjectDetailBean.getLivestatus().equals("init")){
                            mViewZhibo.setVisibility(View.GONE);
                        }else{
                            mViewZhibo.setVisibility(View.VISIBLE);
                            if(subjectDetailBean.getLivestatus().equals("playback")){
                                mViewZhibo.setText("观看回放");
                            }else if(subjectDetailBean.getLivestatus().equals("playbacking")){
                                mViewZhibo.setText("观看回放");
                                mViewZhibo.setEnabled(false);
                            }else{
                                mViewZhibo.setText("观看直播");
                            }
                        }
                    } else {
                        toast(response.body().getMsg());
                    }
                } else {
                    toast("获取套餐详情失败");
                }
            }

            @Override
            public void onFailure(Call<GetSubjectResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }


}
